package controller

import (
	"errors"
	"github.com/gin-gonic/gin"
	"gitlab.local/DO-module/new-filemanage-module/src/core/middleware"
	"gitlab.local/DO-module/new-filemanage-module/src/service/filesystem"
	//"gitlab.local/TerraMaster/tos-modules/middleware"
	"gitlab.local/golibrary/resp"
	"gitlab.local/golibrary/utils"
)

type FileTool struct {
	fSys *filesystem.FileSystem
}

func InitFileToolsRouter(engine *gin.Engine,
	checker middleware.AuthChecker,
	fSys *filesystem.FileSystem) *FileTool {

	ft := FileTool{}
	ft.fSys = fSys

	fileManage := engine.Group("/fileManage", checker.LoginAuthed())
	{
		fileManage.POST("/getSearchFiles", resp.WrapResp(ft.handleSearchFiles)) //搜索文件
	}

	return &ft
}

// handleSearchFiles 搜索文件
func (ft *FileTool) handleSearchFiles(c *gin.Context) (interface{}, error) {
	if err := c.BindJSON(&ft.fSys.FileTools.FileTool); err != nil {
		return nil, err
	}
	username, err := middleware.GetUserName(c)
	if err != nil {
		return nil, err
	}
	conf, err := ft.fSys.FileNav.GetRecentlyConf(username)
	if err != nil {
		return nil, err
	}
	//切换执行用户
	if e := utils.SwitchUserRight(username); e != nil {
		return nil, errors.New("failed to obtain permission")
	}
	return ft.fSys.FileTools.SearchFiles(conf.NoShowHiddenFolder)
}
